package com.erp.order.purchasing.controller;

import com.erp.hr.base.controller.BaseController;
import com.erp.order.purchasing.dao.InquiryListSupplierDao;
import com.erp.order.purchasing.model.InquiryListSupplier;
import com.erp.order.purchasing.model.QuotedPrice;
import com.erp.order.purchasing.model.QuotedPriceDetail;
import com.erp.order.purchasing.service.PurchaseOrderService;
import com.erp.order.purchasing.service.QuotedPriceDetailService;
import com.erp.order.purchasing.service.QuotedPriceService;
import org.redragon.framework.util.ShiroUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("web/quotedPrice")
public class QuotedPriceController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(QuotedPriceController.class);
    /*
     * 服务注入
     * */
    @Autowired
    private QuotedPriceService quotedPriceService;

    @Autowired
    private QuotedPriceDetailService quotedPriceDetailService;

    @Autowired
    private InquiryListSupplierDao inquiryListSupplierDao;

    @Autowired
    private PurchaseOrderService purchaseOrderDao;



    @ResponseBody
    @RequestMapping("save")
    public Map<String,String> save(@RequestBody QuotedPrice quotedPrice) {
        Map map=new HashMap();
        int count=0;
        quotedPrice.setCreateTime(new Date());
        quotedPrice.setCreateUser(ShiroUtil.getPrincipal());
        this.quotedPriceService.addCompleteData(quotedPrice);

        //修改供应商报价
        InquiryListSupplier inquiryListSupplier=new InquiryListSupplier();
        inquiryListSupplier.setInquiryListId(Long.valueOf(quotedPrice.getInquiryListId()));
        inquiryListSupplier.setSupplierId(Long.valueOf(quotedPrice.getVendorId()));
        inquiryListSupplier=inquiryListSupplierDao.getDataObjects(inquiryListSupplier).get(0);
        //更新报价金额
        inquiryListSupplier.setBidAmount(quotedPrice.getTotal());
        inquiryListSupplier.setId(inquiryListSupplier.getId());
        inquiryListSupplierDao.updateDataObject(inquiryListSupplier);

        for (QuotedPriceDetail detail:quotedPrice.getQuotedPriceDetail()) {
            String s= String.valueOf(quotedPrice.getId());
            detail.setQuotedPriceId(s);
            detail.setCreateTime(new Date());
            detail.setCreateUser(ShiroUtil.getPrincipal());
            this.quotedPriceDetailService.saveData(detail);
            count++;
        }
        System.out.print(count);
        if(count==quotedPrice.getQuotedPriceDetail().size()){
            //单一询价单转化为采购单
            String inquiryType=quotedPrice.getType();
            if(inquiryType.equals("0")){
                this.purchaseOrderDao.inquiryListToPurchaseOrder(Long.valueOf(quotedPrice.getInquiryListId()));
            }
            map.put("result","success");
            map.put("message","报价成功！");
        }else{
            map.put("result","fail");
            map.put("message","报价失败！");
        }
        return map;
    }

    /*判断是否已报价*/
    @ResponseBody
    @RequestMapping("check")
    public int check(@RequestBody QuotedPrice inquiryId) {
        //当前登录人的登录名
        String username= ShiroUtil.getPrincipal();
        //String username= "8978iyuh";
        //String username= "123123wA998iidda";
        //String username= "sffvtrbgnj";
        List<QuotedPrice> list=this.quotedPriceService.getData(inquiryId.getInquiryListId(),username);
        return list.size();
    }


    @RequestMapping("updateState")
    public String updateState(QuotedPrice quotedPrice) {
        quotedPrice.setStatus("0");
        this.quotedPriceService.insertOrUpdateDataObject(quotedPrice);
        return "basic.jsp?content=mdVendor/myOrders";
    }
}
